home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 13508 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  3.6 KB

  1. Path: ibm32.perftech.com!usenet
  2. From: murf@perftech.com (John Murphy)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: What is byte-alignment?
  5. Date: 8 Apr 1996 12:34:40 GMT
  6. Organization: Performance Technology Inc
  7. Message-ID: <4kb150$6eu@ibm32.perftech.com>
  8. References: <4jprfe$c6q@alcor.usc.edu>
  9. NNTP-Posting-Host: k5zba.perftech.com
  10. Mime-Version: 1.0
  11. NNTP-Posting-User: REVCO
  12. X-Newsreader: WinVN 0.93.11
  13.  
  14. In article <4jprfe$c6q@alcor.usc.edu>, wawda@alcor.usc.edu says...
  15. >
  16. >I often hear the phrase "byte-aligment" in this newsgroups, yet have
  17. >no clue what it is. Can someone please explain it to me? It comes up
  18. >often when people talk about structures. Thanks in advance,
  19. >
  20. >A. Wawda
  21. >wawda@scf.usc.edu
  22. >
  23.     Byte-alignment has to do with the address of an object relative to its
  24.     size; bytes are always aligned, words are aligned only if they are
  25.     located at even addresses, and double words are aligned only if they are
  26.     located at addresses that are an even multiple of four.
  27.  
  28.     The importance of byte-alignment is that byte-aligned objects can be
  29.     accessed with a single memory reference.  Consider the simple case of a
  30.     16-bit machine with a 16-bit memory system (in which case we don't have
  31.     to worry about double words, since they're not a fundamental data type
  32.     of the machine): each memory read operation returns 16 bits (two bytes)
  33.     of data.  If you read a single byte at an even address, the neighboring
  34.     byte at the next higher (odd) address is also read, but discarded; if
  35.     you read a single byte at an odd address, the neighboring byte at the
  36.     next lower (even) address is also read, but discarded.  If you read a
  37.     word at an even address, both bytes are read at once; but if you try to
  38.     read a word at an odd address, things get more complicated!  To read a
  39.     word from address three, for instance, would require reading the pair of
  40.     bytes at addresses two and three; discarding the bytes at address two
  41.     and saving the byte at address three, shifted eight bits, for later;
  42.     reading the pair of bytes at addresses four and five; discarding the
  43.     byte at address five; and combining the byte at address four, shifted
  44.     eight bits, with the saved and shifted byte from address three!  Now,
  45.     depending on your machine, that's either not quite as bad as it sounds,
  46.     or much worse than it sounds.  On some machines, including Intel based
  47.     machines, the hardware takes care of all the details and the only
  48.     penalty of reading a word on an odd boundary is the extra time required
  49.     for the extra memory access; on other machines, however, the hardware
  50.     simply refuses to deal with such a "complicated" operation, and your
  51.     program is terminated with a nasty message about your attempt to access
  52.     unaligned data.
  53.  
  54.     C compilers normally hide all the ugliness of byte-alignment from us by
  55.     padding variables, structures, and malloc'ed data blocks so that
  56.     everything is 'properly' aligned; this insures maximum efficiency (in
  57.     some sense, at least) as well as insuring operation on the brain damaged
  58.     machines that don't support non-aligned access.  But there are a few
  59.     cases where you need to be very aware of byte-alignment issues.  One
  60.     such case is when you build structures on the fly in malloc'ed or shared
  61.     memory, in which case you may want (or need) to provide your on padding
  62.     to keep things aligned.  Another case is when you're dealing with
  63.     externally defined data such as communication headers of disk data
  64.     structures; if you don't disable your compiler's alignment padding, your
  65.     structure may not match the actual data!
  66.     
  67.     Murf
  68.  
  69.